package at.fhj.swd.pse.beans;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.logging.Logger;

import at.fhj.swd.pse.domain.NewsService;
import at.fhj.swd.pse.entities.News;

@Named
@SessionScoped
public class NewsBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = Logger.getLogger(NewsBean.class);

	@Inject
	private LoginBean loginBean;

	@Inject
	private NewsService newsService;

	private News newNews;

	private News selectedNews;

	@PostConstruct
	public void init() {
		this.newNews = new News();
	}

	public List<News> getNewsList() {
		return this.newsService.getAllOrderedByCreation();
	}

	public String createNews() {
		logger.debug("Create news");

		this.newNews.setCreator(this.loginBean.getLoggedInUser());
		this.newNews.setTimestamp(new Date(System.currentTimeMillis()));

		this.newsService.insertNews(this.newNews);
		return "news.xhtml";
	}

	public String editNews() {
		this.newsService.updateNews(this.selectedNews);
		return "news.xhtml";
	}

	public News getNewNews() {
		return this.newNews;
	}

	public void setNewNews(final News newNews) {
		this.newNews = newNews;
	}

	public News getSelectedNews() {
		return this.selectedNews;
	}

	public void setSelectedNews(final News selectedNews) {
		this.selectedNews = selectedNews;
	}
}
